package com.espirit.eap.sa;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.espirit.EntityMapping;
import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.annotation.ModelDisplayProperty;
import com.googlecode.cswish.struts.hibernate.ISite;
import com.googlecode.cswish.util.bean.ModelUtil;

@Entity
@DomainModel
@ModelDisplayProperty(value="name")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Website extends EntityMapping implements ICheckModelDuplicateInfo, Serializable, ISite {

	private static final long serialVersionUID = -4389223056404284755L;
	
	public final static Integer STATUS_DISABLE = 0;
	public final static Integer STATUS_ACTIVE = 1;
	public final static Integer ACTIVE_STATUS_UPGRADE = 3;
	public final static Integer ACTIVE_STATUS_STARTUP = 4;
	// TODO: remove this status phase?
	public final static Integer STATUS_INIT = 2;
	public final static Integer STATUS_STOP = 5;
	
	
//	private String name;
	
	private String loginPage;
	
	private String homePage;

	// A flag to identify the site can treat as template
	private Boolean templateSite;
	
	private List<Website> websiteTemplates;
	
	private Integer userCount;
	
	private Date createdOn;
	
	private Date modifyOn;
	
	private Date validDate;
	
	private Integer status;
	
	private String productName;
	
	/**
	 * Administrator password
	 */
	private String password;

	private Integer compId; //sid for others
	@Column(nullable=false, unique=true)
	public Integer getCompId() {
		return compId;
	}

	public void setCompId(Integer compId) {
		this.compId = compId;
	}


//	@Column(nullable=false, unique=true)
	@Transient
	public String getName() {
		return compId.toString();
	}

//	public void setName(String name) {
//		this.name = name;
//	}

	public String getLoginPage() {
		return loginPage;
	}

	public void setLoginPage(String loginPage) {
		this.loginPage = loginPage;
	}

	public String getHomePage() {
		return homePage;
	}

	public void setHomePage(String homePage) {
		this.homePage = homePage;
	}

	public Integer getUserCount() {
		return userCount;
	}

	public void setUserCount(Integer userCount) {
		this.userCount = userCount;
	}

	public Date getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	public Date getModifyOn() {
		return modifyOn;
	}

	public void setModifyOn(Date modifyOn) {
		this.modifyOn = modifyOn;
	}

	public Date getValidDate() {
		return validDate;
	}

	public void setValidDate(Date validDate) {
		this.validDate = validDate;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public Boolean getTemplateSite() {
		return templateSite;
	}

	public void setTemplateSite(Boolean templateSite) {
		this.templateSite = templateSite;
	}

	@ManyToMany(targetEntity=Website.class, fetch=FetchType.LAZY)
	@JoinTable(name = "saas_website_websitetemplate")
	public List<Website> getWebsiteTemplates() {
		return websiteTemplates;
	}

	public void setWebsiteTemplates(List<Website> websiteTemplates) {
		this.websiteTemplates = websiteTemplates;
	}

	// TODO: transient or use a field to store?
	@Transient
	public Website getDefaultTemplate() {
		if (websiteTemplates != null && websiteTemplates.size() > 0) {
			return websiteTemplates.get(0);
		} else {
			return null;
		}
	}

	private final static String[] gatherProperties = new String[]{"name"};
	@Override
	public String[] gatherProperties() {
		return gatherProperties;
	}
	
	public Website clone() {
		Website website = new Website();
		ModelUtil.copyIgnoreNullValue(this, website);
		
		if (websiteTemplates != null) {
			// only copy the first level template
			List<Website>  cloneWebsiteTemplates = new ArrayList<Website>(websiteTemplates.size());
			for (Website websiteTemmplate : websiteTemplates) {
				Website cloneSiteTemplate = new Website();
				ModelUtil.copyIgnoreNullValue(websiteTemmplate, cloneSiteTemplate);
				cloneSiteTemplate.setWebsiteTemplates(null);
				cloneWebsiteTemplates.add(cloneSiteTemplate);
			}
			website.setWebsiteTemplates(cloneWebsiteTemplates);
		}
		return website;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}
	
	private String companyName;
	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	
}
